---
title: "Study 1: Replication File"
author: "ASD"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
knitr::opts_chunk$set(message = F)
knitr::opts_chunk$set(warning = F)

```

```{r}
#load libraries
library(pwr)
library(plm)
library(effectsize)
library(stargazer)
library(sandwich)
library(equivtest) #from install_github("ekhartman/equivtest")
library(tidyverse)

### Data
blm_dat <- read_rds("blm_replication_dat.rds") 

### Functions
robust_se <- function(x) {sqrt(diag(vcovHC(x, type = "HC0")))}

wrapper <- function(x, ...) 
{
  paste(strwrap(x, ...), collapse = "\n")
}

```

## Main Analyses 

### H1 (Fig 2) Does the placement of Sensitive Items (RR) Change the Measurement of RR?

```{r}
blm_dat %>% 
  mutate(placement = placement %>% factor(levels = c("Post", "Pre"))) %>% 
  group_by(placement) %>% 
  summarize(avg_racial = mean(racial, na.rm = T), 
            dev_racial = sd(racial, na.rm = T), 
            n = n()) %>% 
  mutate(mi = 1.96 * (dev_racial/sqrt(n)),
         plus = avg_racial + mi,
         minus = avg_racial - mi) %>%
  ggplot(aes(x = placement, y = avg_racial, color = placement, shape = placement)) +
  geom_point(size = 3) + 
  geom_errorbar(aes(ymin = minus, ymax = plus), width = .1, size = 1.2) + 
  theme_bw() +
  coord_flip() +
  scale_shape_manual(
    name = "Placement of Sensitive Items",
    values = c(2, 1),
  ) + 
    scale_color_manual(
    name = "Placement of Sensitive Items",
    values = c("grey42", "grey65"),
  ) +
  labs(x = "", y = "Mean Racial Resentment") +
    theme(axis.text=element_text(size=13),
        axis.title=element_text(size=13),
        legend.text=element_text(size=9),
        legend.title=element_text(size=11),
        legend.position = "none",
        legend.key.width = unit(1.5,"cm"),
        legend.spacing.x = unit(0.25,"cm"),
        legend.justification = "center") +
  ylim(0,1)
```

#### Corresponding LM (Appendix B.5, Table B.4)


```{r}
blm_dat %>%  
  lm(formula = racial ~ placement) %>% 
  stargazer(type = "text", se = list(robust_se(.)), covariate.labels = c("Placement of RR = Post"), dep.var.labels = c("Level of Racial Resentment (RR)"), report=("vc*s*p"),   star.cutoffs = c(0.05, 0.01, 0.001))
```

### H2 (Fig 3) Does the Placement of Sensitive Items (RR) Impact Support for Goals of BLM?

```{r}
blm_dat %>% 
  mutate(placement = placement %>% factor(levels = c("Post", "Pre"))) %>% 
  group_by(placement) %>% 
  summarize(avg_goals = mean(goals), 
            dev_goals = sd(goals, na.rm = T), 
            n = n()) %>% 
  mutate(mi = 1.96 * (dev_goals/sqrt(n)),
         plus = avg_goals + mi,
         minus = avg_goals - mi) %>%
  ggplot(aes(x = placement, y = avg_goals, color = placement, shape = placement)) +
  geom_point(size = 3) + 
  geom_errorbar(aes(ymin = minus, ymax = plus), width = .1, size = 1.2) + 
  theme_bw() +
  coord_flip() +
  scale_shape_manual(
    name = "Placement of Sensitive Items",
    values = rev(c(1, 2)),
  ) +  
  scale_color_manual(
    name = "Placement of Sensitive Items",
    values = c("grey42", "grey65"),
  ) +
  labs(x = "", y = "Mean Support for BLM") +
    theme(axis.text=element_text(size=13),
        axis.title=element_text(size=13),
        legend.text=element_text(size=9),
        legend.title=element_text(size=11),
        legend.position = "none",
        legend.key.width = unit(1.5,"cm"),
        legend.spacing.x = unit(0.25,"cm"),
        legend.justification = "center") +
  ylim(0,1)
```

#### Corresponding LM (Appendix B.5, Table B.4)

```{r}
blm_dat %>% 
  lm(formula = goals ~ placement) %>% 
  stargazer(type = "text", se = list(robust_se(.)), covariate.labels = c("Placement of RR = Post"), dep.var.labels = c("Support for Goals of BLM"), report = ("vc*s*p"),   star.cutoffs = c(0.05, 0.01, 0.001))
```


### H3 (Fig 4) Does the Placement of Sensitive Items (RR) and the Substantive Treatment Impact Support for Goals of BLM?

```{r}
blm_dat %>% 
  mutate(placement = placement %>% factor(levels = c("Post", "Pre"))) %>% 
  group_by(placement, treatment, condition) %>% 
  summarize(avg_goals = mean(goals), 
            dev_goals = sd(goals), 
            n = n()) %>% 
  mutate(mi = 1.96 * (dev_goals/sqrt(n)),
         plus = avg_goals + mi,
         minus = avg_goals - mi) %>% 
  ggplot(aes(x = treatment, y = avg_goals, shape = placement, color = placement)) + 
  geom_point(position = position_dodge(0.3), size = 3) +
  geom_line(position = position_dodge(0.3), size = 1.2) +
  geom_errorbar(aes(ymin = minus, ymax = plus), width = .1, position = position_dodge(0.3), size = 1.2) +
   scale_shape_manual(
    name = "Placement of Sensitive Items",
    values = rev(c(1, 2)),
  ) +  
  scale_color_manual(
    name = "Placement of Sensitive Items",
values = c("grey42", "grey65"),
  ) +
  theme_bw() +
  coord_flip() +
  labs(x = "", y = "Mean Support for BLM")+ 
  theme(axis.text=element_text(size=13),
        axis.title=element_text(size=13),
        legend.text=element_text(size=9),
        legend.title=element_text(size=11),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        legend.key.width = unit(1.5,"cm"),
        legend.spacing.x = unit(0.25,"cm"),
        legend.justification = "center") +
  guides(shape = guide_legend(title.position="top", 
                                    reverse = T), 
               color = guide_legend(title.position="top", 
                                    reverse = T)
        )+
  ylim(0,1)
```

#### Corresponding LM (Appendix B.5, Table B.5)
```{r}
blm_dat %>% 
  lm(formula = goals ~ treatment*placement) %>% 
  stargazer(type = "text",
  se = list(robust_se(.)),
  covariate.labels = c("Treatment = Feminist", "Placement = Post", "Post x Feminist", "Constant"),
  dep.var.labels = c("Support for Goals of BLM"),
  report = ("vc*s*p"),
  star.cutoffs = c(0.05, 0.01, 0.001)
  )
```

#### Difference in Means (Appendix B.5, Table B.6)

```{r}
t.test(blm_dat$goals[blm_dat$condition == "Pre Feminist"], blm_dat$goals[blm_dat$condition == "Post Feminist"])

t.test(blm_dat$goals[blm_dat$condition == "Pre Nationalist"], blm_dat$goals[blm_dat$condition == "Post Nationalist"])

t.test(blm_dat$goals[blm_dat$condition == "Pre Feminist"], blm_dat$goals[blm_dat$condition == "Pre Nationalist"])

t.test(blm_dat$goals[blm_dat$condition == "Post Feminist"], blm_dat$goals[blm_dat$condition == "Post Nationalist"])
```

### H4 (Fig 5) Does the placement of sensitive items (RR), the measurement of the sensitive items (RR), and the substantive treatment impact Support for Goals of BLM?

```{r}
blm_dat %>% 
  mutate(placement = placement %>% factor(levels = c("Post", "Pre")),
    racial_hl = ifelse(racial < 0.5, "Low Racial Resentment", "High Racial Resentment") %>% as.factor() %>% factor(levels = c("Low Racial Resentment", "High Racial Resentment"))) %>% 
  filter(!is.na(racial_hl)) %>% 
  group_by(placement, treatment, racial_hl) %>% 
  summarize(avg_goals = mean(goals), 
            dev_goals = sd(goals), 
            n = n()) %>% 
  mutate(mi = 1.96 * (dev_goals/sqrt(n)),
         plus = avg_goals + mi,
         minus = avg_goals - mi) %>% 
  ungroup() %>% 
  ggplot(aes(y = treatment, x = avg_goals, shape = placement, color = placement)) + 
  geom_point(position = position_dodge(0.3), size = 3) +
  geom_line(position = position_dodge(0.3), size = 1.2) +
  geom_errorbar(aes(xmin = minus, xmax = plus), width = .1, position = position_dodge(0.3), size = 1.2) + 
  theme_bw() +
  scale_shape_manual(
    name = "Placement of Sensitive Items",
    values = rev(c(1, 2)),
  ) +  
  scale_color_manual(
    name = "Placement of Sensitive Items",
    values = rev(c("grey65", "grey42")),
  ) +
  theme_bw() +
  facet_wrap(~racial_hl) +
  labs(y = "", x = "Mean Support for BLM") +
    theme(axis.text=element_text(size=13),
        axis.title=element_text(size=13),
        axis.ticks.y=element_blank(),
        legend.text=element_text(size=9),
        legend.title=element_text(size=11),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        legend.key.width = unit(1.5,"cm"),
        legend.spacing.x = unit(0.25,"cm"),
        legend.justification = "center", 
        panel.spacing.x = unit(1, "lines")) +
  guides(shape = guide_legend(title.position="top", 
                                    reverse = T), 
               color = guide_legend(title.position="top", 
                                    reverse = T)
        ) +
  xlim(0,1)
```

#### Corresponding LM (Appendix B.5, Table B.7)

```{r}
blm_dat %>% 
  lm(formula = goals ~ treatment*placement*racial) %>% 
  stargazer(type = "text",
  se = list(robust_se(.)),
  covariate.labels = c("Pre x Feminist", "Post x Nationalist","Racial Resentment (RR)", "Post x Feminist", "RR x Pre x Feminist", "RR x Post x Nationalist", "RR x Post x Feminist", "Constant"),
  dep.var.labels = c("Support for Goals of BLM"),
  report = ("vc*s*p"),
  star.cutoffs = c(0.05, 0.01, 0.001)
  )
```

#### Difference in Means (Appendix B.5, Table B.8)

```{r}
blm_3tt <- blm_dat %>% 
  mutate(racial_hl = ifelse(racial < 0.5, "Low", "High")) %>% 
  unite("racial_condition", racial_hl, condition) 

#low

t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Feminist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Nationalist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Nationalist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Nationalist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Nationalist"])

#high

t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Feminist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Nationalist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Nationalist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Nationalist"])

t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Post Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Nationalist"])

```

## Power (Appendix B.6)

```{r}
pwr.anova.test(k = 2, n = 442, sig.level = 0.05, power = 0.8)
```

## Equivalence Test (Appendix B.7, Table B.9)
```{r}
  
### H1: Placement of sensitive item on measurement of that item
data1 <- blm_dat$racial[blm_dat$placement == "Pre"]
data2 <- blm_dat$racial[blm_dat$placement == "Post"]
equiv.t.test(data1, data2, eps_std = 0.157) %>% summary()

### H2: Placement on sensitive item on measurement of the experimental DV
data1 <- blm_dat$goals[blm_dat$placement == "Pre"]
data2 <- blm_dat$goals[blm_dat$placement == "Post"]
equiv.t.test(data1, data2, eps_std = 0.224) %>% summary()

### H3: Placement of sensitive item * experimental condition on measure of experimental DV 
data1 <- blm_dat$goals[blm_dat$condition == "Pre Feminist"]
data2 <- blm_dat$goals[blm_dat$condition == "Post Feminist"]
equiv.t.test(data1, data2, eps_std = 0.249) %>% summary()

data1 <- blm_dat$goals[blm_dat$condition == "Pre Nationalist"]
data2 <- blm_dat$goals[blm_dat$condition == "Post Nationalist"]
equiv.t.test(data1, data2, eps_std = 0.301) %>% summary()

data1 <- blm_dat$goals[blm_dat$condition == "Pre Feminist"]
data2 <- blm_dat$goals[blm_dat$condition == "Pre Nationalist"]
equiv.t.test(data1, data2, eps_std = 0.338) %>% summary()

data1 <- blm_dat$goals[blm_dat$condition == "Post Feminist"]
data2 <- blm_dat$goals[blm_dat$condition == "Post Nationalist"]
equiv.t.test(data1, data2, eps_std = 0.276) %>% summary()


### H4: Placement of sensitive item * experimental condition on measure of experimental DV 
#low
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Feminist"], eps_std = 0.476) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Nationalist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Nationalist"], eps_std = 0.253) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Pre Nationalist"], eps_std = 0.325) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "Low_Post Nationalist"], eps_std = 0.503) %>% summary()

#high
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Feminist"], eps_std = 0.324) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Nationalist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Nationalist"], eps_std = 0.397) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Pre Nationalist"], eps_std = 0.521) %>% summary()
equiv.t.test(blm_3tt$goals[blm_3tt$racial_condition == "High_Post Feminist"], blm_3tt$goals[blm_3tt$racial_condition == "High_Post Nationalist"], eps_std = 0.171) %>% summary()

```